<?php

require_once dirname(__FILE__).'/../lib/BasesfGuardUserActions.class.php';

/**
 * sfGuardUser actions.
 *
 * @package    sfGuardPlugin
 * @subpackage sfGuardUser
 * @author     Fabien Potencier
 * @version    SVN: $Id: actions.class.php 12965 2008-11-13 06:02:38Z fabien $
 */
class sfGuardUserActions extends basesfGuardUserActions
{
	
	
//	 public function executeEdit(sfWebRequest $request)
// {
 //  $this->sf_guard_user = $this->getRoute()->getObject();
   
 //  $this->form = new sfGuardUserForm();
// }

protected function buildCriteria()
  {
    if (is_null($this->filters))
    {
      $this->filters = $this->configuration->getFilterForm($this->getFilters());
    }

    $criteria = $this->filters->buildCriteria($this->getFilters());

    $this->addSortCriteria($criteria);

    $event = $this->dispatcher->filter(new sfEvent($this, 'admin.build_criteria'), $criteria);
    $criteria = $event->getReturnValue();
    $criteria->add(sfGuardUserPeer::IS_PARTNER,false);
    return $criteria;
  }

public function executeMailing(sfWebRequest $request)
  {
  	
  	/*
  	 * отправляем письма всем мужчинам
  	 */
  	if($request->getParameter('int_letter')) {
  		$user = sfGuardUserPeer::retrieveByPK($user=$request->getParameter('id_user'));
  		//$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' )
  		$c = new Criteria ( );
		$c->add ( sfGuardUserPeer::GENDER, 'm' );
		$cnt = sfGuardUserPeer::doSelect( $c );
		
		foreach ($cnt as $cnt) {

				//echo "-".$cnt->getId()."<br>";
				$m=new Mailbox();
				$m->setFromId($user->getId());
				$m->setToId($cnt->getId());
				$m->setBody($user->getIntroductionLetter());
				$m->setPub(1);
				$m->save();

				//// Шлем уведомление	
				$not = MailnoticePeer::retrieveByPK ( 3 );
				$title = $not->getTitle ();
				$body = $not->parse_body ( array ('%from%' => $user->getUsername(), '%link%' => "<a href='" . sfConfig::get ( 'app_site_url' ) . url_for ( '@mailbox_input' ) . "'>" . __ ( 'ссылку' ) . "</a>" ) );
				myTools::sendMail ( $cnt->getEmail(), $title, $body );				
				//echo $cnt->getEmail().'---'.$title.'---'.$body;
		
		}
  	}
  	
  	
  }	
	
  
}
